package com.coreJava.test02.xiancheng;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 作者: LCG
 * 日期: 2019/9/4 10:27
 * 描述:  线程池的操作
 */
public class MyPoolOfThread {

    public static void main(String[] args) throws InterruptedException {

        ArrayBlockingQueue<Runnable> arrayBlockingQueue=new ArrayBlockingQueue<>(1);

        LinkedBlockingQueue<Runnable> linkedBlockingQueue=new LinkedBlockingQueue<>();

        MyRejectedExecttionHandler myRejectedExecttionHandler=new MyRejectedExecttionHandler(linkedBlockingQueue);

        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(
                2,
                4,
                5,
                TimeUnit.SECONDS,
                arrayBlockingQueue,
                myRejectedExecttionHandler
                );

        FutureTask<String> futureTask=new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {

                System.out.println("====进入任务===");
                Random random = new Random();
                int i = random.nextInt(10000);
                Thread.sleep(i);

                return ""+i;
            }
        });

        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);
        threadPoolExecutor.execute(futureTask);

        //System.out.println(threadPoolExecutor.getActiveCount());

        //System.out.println(arrayBlockingQueue.size());

        //System.out.println(linkedBlockingQueue.size());

        threadPoolExecutor.shutdown();

        while (true){

            System.out.println("======");
            Runnable take = linkedBlockingQueue.take();

            take.run();
        }

    }

}
